<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box{
      text-align: center;
      width: 980px;
      margin: 0 auto;
    }
    .tab{
      margin-bottom: 10px;
    }
    .tab span{
      display: inline-block;
      width: 80px;
      line-height: 50px;
      height: 50px;
      margin-left: 10px;
    }
    .tab span a{
      text-decoration: none;
      color: #000;
    }
    .content{
      
      width: 500px;
      margin: 0 auto;
      height: 300px;
      border: 1px solid red;
    }
    .tab span .active{
      color: red;
      border-bottom: 3px solid red;
    }
    .item{
      display: none;
    }
    .content .active{
      display: block;
    }
  </style>
</head>
<body>
  <div class="box">
    <div class="tab">
      <span><a class="active" href="javascript:;">零食</a></span>
      <span><a href="#">饮料</a></span>
      <span><a href="#">生活用品</a></span>
    </div>
    <div class="content">
      <div class="item active">零食内容</div>
      <div class="item">饮料内容</div>
      <div class="item">生活用品内容</div>
    </div>
  </div>
  <script>
    const as = document.querySelectorAll('.tab a')
    for (let i = 0; i < as.length; i++){
        as[i].addEventListener('mouseenter',function(){
          // console.log('1111');
          document.querySelector('.tab .active').classList.remove('active')
          this.classList.add('active')
          document.querySelector('.content .active').classList.remove('active')
          document.querySelector(`.content .item:nth-child(${i+1})`).classList.add('active')
        })
    }
  </script>
</body>
</html>